package se.webbzon.boltzmann.game.object.spawner;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class AbstractNPCSpawnerDeflation extends AbstractSpawnerDeflation {

	private String characterTextureSetName;
	
	/*============================================================
	Constructors
	============================================================*/

	/** Creates a new empty abstract non-player character 
	 * spawner deflation. **/
	public AbstractNPCSpawnerDeflation(int deflationProperties) {
		super(deflationProperties, null);
	}
	
	/** Creates a new abstract non-player character spawner deflation of an
	 * existing abstract non-player character spawner. **/
	public AbstractNPCSpawnerDeflation(	int deflationProperties,
										AbstractNPCSpawner spawner) {
		super(deflationProperties, null, spawner);
		this.characterTextureSetName = spawner.getCharacterTextureSetName();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the name of the character texture set. **/
	public String getCharacterTextureSetName() {
		return characterTextureSetName;
	}
	
	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		
		// Read the character texture set name
		byte[] data = new byte[buff.getShort()];
		buff.get(data,0,data.length);
		try {
			characterTextureSetName = new String(data,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			characterTextureSetName = "";
		}
	}

	@Override public ByteBuffer deflate(int allocate) {
		byte[] data;
		try {
			data = characterTextureSetName.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			data = new byte[0];
		}
		
		ByteBuffer buff = super.deflate(allocate + 2 + data.length);
		
		// Write the character texture set name
		buff.putShort((short) data.length);
		buff.put(data);
		
		return buff;
	}

}
